package it.unipd.dei.bd.icineasti.database;

import it.unipd.dei.bd.icineasti.resource.Tariffa;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/** 
 * Restituisce tutte le istanze in tariffa (valide per un biglietto singolo)
 *
 * @author Morris Antonello
 */
public class GetTariffeDatabase {

	/**
	 * Interrogazione al database.
	 */
	private static final String STATEMENT = "SELECT Fascia, CAST(PrezzoListino AS NUMERIC), In3D  " +
											"FROM Tariffa";
	
	/**
	 * Connessione al database
	 */
	private final Connection con;
	
	/**
	 * Risultati della ricerca
	 */
	private final List<Tariffa> tariffe;
	
	public GetTariffeDatabase(Connection con) {
		this.con = con;
		tariffe = new ArrayList<Tariffa>();
	}

	public List<Tariffa> getTariffe() throws SQLException {

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			pstmt = con.prepareStatement(STATEMENT);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				tariffe.add(
							new Tariffa(
										rs.getString("Fascia"), 
										rs.getBigDecimal("PrezzoListino"), 
										rs.getBoolean("In3D")
										)
							);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (pstmt != null) {
				pstmt.close();
			}

			con.close();
		}

		return tariffe;
	}
}
